DELETE blogs

PUT /blogs/_doc/1
{
    "title": "Quick brown rabbits",
    "body":  "Brown rabbits are commonly seen."
}

PUT /blogs/_doc/2
{
    "title": "Keeping pets healthy",
    "body":  "My quick brown fox eats rabbits on a regular basis."
}

// 肉眼可见, 文档2 更加符合要求, 但是用bool查询文档1, 却在前面
// title body相互竞争, 不应该将分数简单的叠加, 而应该找到单个最佳匹配的字段评分
POST /blogs/_search
{
    "query": {
        "bool": {
            "should": [
                { "match": { "title": "Brown fox" }},
                { "match": { "body":  "Brown fox" }}
            ]
        }
    }
}

// disjuction max query, 就是取多字段查询中, 最匹配的评分作为最终的评分返回, 而不是相加
POST blogs/_search
{
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "Brown fox" }},
                { "match": { "body":  "Brown fox" }}
            ]
        }
    }
}


// 问题: 此查询两个分数一样, 肉眼可见文档2 body字段 比文档1 多一个quick, 
POST blogs/_search
{
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "Quick pets" }},
                { "match": { "body":  "Quick pets" }}
            ]
        }
    }
}

// 解决: 增加 tie_breaker 参数来调整
// 获取最佳匹配的评分 _score -> 将其他的匹配语句的评分和 tie_breaker 相乘 -> 求和并规范化
// tie_breaker 范围 [0, 1], 如果取0的话, 取最佳匹配评分, 1的话就是所有都一样
POST blogs/_search
{
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "Quick pets" }},
                { "match": { "body":  "Quick pets" }}
            ],
            "tie_breaker": 0.2
        }
    }
}